<template>
  <div style="flex: 1;display: flex;" :key="forceKey">
    <vue-draggable-resizable classNameResizable="my-resize" :w="fileTree.aaa" :min-width="100" :max-width="400"
                             style="height:100%" :draggable="false" :handles="['mr']" :parent="true">
      <div class="left-container" @drop="dropFile($event)" v-loading="loading"
           @dragover="dragOverHandler($event)">
        <template v-if="publicTree.newChildren&&publicTree.newChildren.length>0">
          <split-pane split="horizontal">
            <template slot="paneL">
              <template v-if="fileTree.newChildren&&fileTree.newChildren.length>0"
                        v-for="(item,key) in fileTree.newChildren" v-show="item.promise.is_display">
                <div
                  :class="key==fileTree.selectKey?'file-button-box file-button-box-selected':'file-button-box'"
                  @click="(event)=>{selectFile(0,key,event)}"
                  @contextmenu="(event)=>{rightSelectFile(0,key,event)}"
                  @dblclick="dbClickFile(0,key)"
                >
                  <div class="file-button-img-box ">
                    <div class="file-button-img" :class="'file-button-img-'+item.inc_type"/>
                  </div>
                  <div class="file-button-title">
                    {{ item.name }}
                  </div>
                  <div
                    v-if="item.newChildren&&item.newChildren.length>0"
                    class="file-button-right"
                  >
                    ﹥
                  </div>
                </div>
              </template>
            </template>
            <template slot="paneR">
              <template v-for="(item,key) in publicTree.newChildren" v-show="item.promise.is_display">
                <div
                  :class="key==publicTree.selectKey?'file-button-box file-button-box-selected':'file-button-box'"
                  @click="selectPublicFile(0,key)"
                  @contextmenu="(event)=>{rightSelectPublicFile(0,key,event)}"
                  @dblclick="dbClickPublicFile(0,key)"
                >
                  <div class="file-button-img-box ">
                    <div class="file-button-img" :class="'file-button-img-'+item.inc_type"/>
                  </div>
                  <div class="file-button-title">
                    {{ item.name }}
                  </div>
                  <div
                    v-if="item.newChildren&&item.newChildren.length>0"
                    class="file-button-right"
                  >
                    ﹥
                  </div>
                </div>
              </template>
            </template>
          </split-pane>
        </template>
        <template v-else>
          <template v-if="fileTree.newChildren&&fileTree.newChildren.length>0"
                    v-for="(item,key) in fileTree.newChildren" v-show="item.promise.is_display">
            <div
              :class="key==fileTree.selectKey?'file-button-box file-button-box-selected':'file-button-box'"
              @click="selectFile(0,key)"
              @contextmenu="(event)=>{rightSelectFile(0,key,event)}"
              @dblclick="dbClickFile(0,key)"
            >
              <div class="file-button-img-box ">
                <div class="file-button-img" :class="'file-button-img-'+item.inc_type"/>
              </div>
              <div class="file-button-title">
                {{ item.name }}
              </div>
              <div
                v-if="item.newChildren&&item.newChildren.length>0"
                class="file-button-right"
              >
                ﹥
              </div>
            </div>
          </template>

        </template>


        <v-contextmenu ref="contextmenu" v-if="fileTree.newChildren&&fileTree.newChildren.length>0">
          <template v-if="fileTree.selectKey!==null&&fileTree.newChildren[fileTree.selectKey]">
            <template v-if="fileTree.newChildren[fileTree.selectKey].inc_type!=='dir'&&fileTree.newChildren[fileTree.selectKey].inc_type!=='cad'&&fileTree.newChildren[fileTree.selectKey].inc_type!=='cadDir'">
              <template v-if="fileTree.newChildren[fileTree.selectKey].promise.is_read==1">
                <v-contextmenu-item
                  @click="$emit('view',fileTree.newChildren[fileTree.selectKey])">{{$t('bs.view')}}
                </v-contextmenu-item>
                <v-contextmenu-item
                  @click="$emit('download',fileTree.newChildren[fileTree.selectKey])">{{$t('bs.download')}}
                </v-contextmenu-item>
                <v-contextmenu-item
                  @click="$emit('shouchang',fileTree.newChildren[fileTree.selectKey])">{{$t('bs.collect')}}
                </v-contextmenu-item>
<!--                <v-contextmenu-item-->
<!--                  @click="$emit('history',fileTree.newChildren[fileTree.selectKey])">历史版本-->
<!--                </v-contextmenu-item>-->
              </template>
              <v-contextmenu-item
                v-if="fileTree.newChildren[fileTree.selectKey].promise.is_share==1"
                @click="$emit('share',fileTree.newChildren[fileTree.selectKey])">{{$t('bs.share')}}
              </v-contextmenu-item>

            </template>
            <template v-else>

              <template v-if="fileTree.newChildren[fileTree.selectKey].inc_type=='cad'">
                <v-contextmenu-item
                  @click="$emit('view',fileTree.newChildren[fileTree.selectKey])">{{$t('bs.view')}}
                </v-contextmenu-item>

              </template>
            </template>
          </template>
        </v-contextmenu>

        <v-contextmenu ref="contextmenu1" v-if="publicTree.newChildren&&publicTree.newChildren.length>0">
          <template v-if="publicTree.selectKey!==null&&publicTree.newChildren[publicTree.selectKey]">
            <template v-if="publicTree.newChildren[publicTree.selectKey].inc_type!=='dir'&&publicTree.newChildren[publicTree.selectKey].inc_type!=='cad'&&publicTree.newChildren[publicTree.selectKey].inc_type!=='cadDir'">
              <template v-if="publicTree.newChildren[publicTree.selectKey].promise.is_read==1">
                <v-contextmenu-item
                  @click="$emit('view',publicTree.newChildren[publicTree.selectKey])">{{$t('bs.view')}}
                </v-contextmenu-item>
                <!--                <v-contextmenu-item -->
                <!--                    @click="$emit('download',publicTree.newChildren[publicTree.selectKey])">{{$t('bs.download')}}-->
                <!--                </v-contextmenu-item>-->
                <v-contextmenu-item
                  @click="$emit('copy',publicTree.newChildren[publicTree.selectKey])">复制
                </v-contextmenu-item>
                <v-contextmenu-item
                  @click="$emit('shouchang',publicTree.newChildren[publicTree.selectKey])">{{$t('bs.collect')}}
                </v-contextmenu-item>
              </template>
              <v-contextmenu-item
                v-if="publicTree.newChildren[publicTree.selectKey].promise.is_share==1"
                @click="$emit('share',publicTree.newChildren[publicTree.selectKey])">{{$t('bs.share')}}
              </v-contextmenu-item>
            </template>
            <template v-else>
              <template v-if="publicTree.newChildren[publicTree.selectKey].inc_type=='cad'">
                <v-contextmenu-item
                  @click="$emit('view',publicTree.newChildren[publicTree.selectKey])">{{$t('bs.view')}}
                </v-contextmenu-item>
              </template>
            </template>
          </template>
        </v-contextmenu>


      </div>
    </vue-draggable-resizable>

    <file-view-search-file
      v-if="fileTree.selectKey!=null&&fileTree.newChildren&&fileTree.newChildren[fileTree.selectKey]&&fileTree.newChildren[fileTree.selectKey].newChildren&&fileTree.newChildren[fileTree.selectKey].newChildren.length>0"
      :file-tree="fileTree.newChildren[fileTree.selectKey]" v-on="$listeners"/>

    <file-view-search-file
      v-if="publicTree.selectKey!=null&&publicTree.newChildren&&publicTree.newChildren[publicTree.selectKey]&&publicTree.newChildren[publicTree.selectKey].newChildren&&publicTree.newChildren[publicTree.selectKey].newChildren.length>0"
      :file-tree="publicTree.newChildren[publicTree.selectKey]" v-on="$listeners"/>

  </div>
</template>
<script>
import splitPane from 'vue-splitpane'
import waves from '@/directive/waves'
import Template from '@/views/bs/template.vue'
import VueDraggableResizable from 'vue-draggable-resizable'
import 'vue-draggable-resizable/dist/VueDraggableResizable.css'
import {updateFile} from "@/api/common";

export default {
  name: 'fileViewSearchFile',
  components: {Template, splitPane, VueDraggableResizable},
  directives: {waves},
  props: {
    fileTree: {
      type: Object,
      default: () => {
        return {}
      }
    },
    publicTree: {
      type: Object,
      default: () => {
        return {}
      }
    },
  },
  data: function () {
    return {
      forceKey: 0,
      loading: false
    }
  },
  methods: {
    dragOverHandler(e) {
      e.preventDefault();
    },
    dropFile(ev) { // 此方法中只读取了一个文件，如果需要批量处理，可改下下方函数中的循环逻辑
      ev.preventDefault();
      console.log('拖拽的文件', ev);
      console.log('节点', this.fileTree)
      if (!this.fileTree.promise || this.fileTree.promise.is_write != 1) {
        this.$message.error("您没有上传到此目录的权限")
        return;
      }
      let file = null;
      if (ev.dataTransfer.items) {
        // Use DataTransferItemList interface to access the file(s)
        for (var i = 0; i < ev.dataTransfer.items.length; i++) {
          // If dropped items aren't files, reject them
          if (ev.dataTransfer.items[i].kind === "file") {
            file = ev.dataTransfer.items[i].getAsFile();
            console.log("... file[" + i + "].name = " + file.name);
          }
        }
      } else {
        // Use DataTransfer interface to access the file(s)
        for (var i = 0; i < ev.dataTransfer.files.length; i++) {
          console.log(
            "... file[" + i + "].name = " + ev.dataTransfer.files[i].name
          );
          file = ev.dataTransfer.files[i];
        }
      }

      this.uploadFile(file);
    },
    // 上传文件
    uploadFile(file) {
      // if(this.fileTree)
      this.loading = true;
      let formData = new FormData();
      formData.append("doc_id", this.fileTree.id);
      formData.append("station_id", this.$store.state.fileView.station_id);
      formData.append("file", file);
      console.log('form', formData)
      updateFile(formData).then((res) => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          let node = res.data[0];
          this.fileTree.newChildren.push(node)
          this.refirsh();


        } else {
          this.$message.error('不支持此种上传方式或上传出错')
        }

      }).catch(() => {
        this.$message.error('不支持此种上传方式或上传出错')
      }).finally(() => {
        this.loading = false;
      })


    },
    refirsh() {
      this.forceKey++
    },
    rightTest(item) {
      console.log(item)
    },
    handleUpload(item) {
      this.$emit('upload', item)
    },
    dbClickFile(index, key) {
      this.fileTree.selectKey = null
      this.$nextTick(() => {
        this.fileTree.selectKey = key
        this.publicTree.selectKey = null;
        if (this.fileTree.newChildren[this.fileTree.selectKey].inc_type !== 'dir') {
          this.$emit('view', this.fileTree.newChildren[this.fileTree.selectKey])
        }
      })


    },
    dbClickPublicFile(index, key) {
      this.publicTree.selectKey = null
      this.$nextTick(() => {
        this.publicTree.selectKey = key
        this.fileTree.selectKey = null;

        if (this.publicTree.newChildren[this.publicTree.selectKey].inc_type !== 'dir') {
          this.$emit('view', this.publicTree.newChildren[this.publicTree.selectKey])
        }


      })
    },
    rightSelectPublicFile(index, key, event) {
      event.preventDefault()
      this.publicTree.selectKey = null
      this.$nextTick(() => {
        this.publicTree.selectKey = key
        this.fileTree.selectKey = null;
        // this.refirsh();
      })

      const {clientX, clientY} = event
      this.$refs.contextmenu1.show({top: clientY, left: clientX})
    },
    rightSelectFile(index, key, event) {
      event.preventDefault()
      this.fileTree.selectKey = null
      this.$nextTick(() => {
        this.fileTree.selectKey = key
        this.publicTree.selectKey = null;
        // this.refirsh();
      })

      const {clientX, clientY} = event
      this.$refs.contextmenu.show({top: clientY, left: clientX})
    },
    selectPublicFile(index, key) {
      this.publicTree.selectKey = null
      this.$nextTick(() => {
        this.publicTree.selectKey = key
        this.fileTree.selectKey = null;
        this.refirsh();
        if (this.publicTree.newChildren[this.publicTree.selectKey].newChildren.length > 0) {
          setTimeout(() => {
            let objDiv = document.getElementById("fileViewScroll");
            objDiv.scrollLeft = objDiv.scrollWidth;

          }, 200)
        }
        console.log(this.publicTree, this.publicTree.selectKey, this.publicTree.newChildren[this.publicTree.selectKey])
      })
    },
    selectFile(index, key, event) {
      console.log('事件', event);
      this.fileTree.selectKey = null
      this.$nextTick(() => {
        this.fileTree.selectKey = key
        this.publicTree.selectKey = null;
        // this.refirsh();
        if (this.fileTree.selectKey !== null && this.fileTree.newChildren && this.fileTree.newChildren[this.fileTree.selectKey].length > 0) {
          setTimeout(() => {
            let objDiv = document.getElementById("fileViewScroll");
            objDiv.scrollLeft = objDiv.scrollWidth;

          }, 200)
        }
        console.log(this.fileTree, this.fileTree.selectKey, this.fileTree.newChildren[this.fileTree.selectKey])
      })
    },
    resize() {

    }
  }
}
</script>

<style scoped>
.tree-container {
  margin-top: 8px;
}

.my-resize {
  display: inline-block;
  position: static;
  border: none;
  border-right: 5px solid #dddddd;
}

.my-resize /deep/ .handle-mr {
  border: none;
  background: #eeeeee;
  height: 100%;
  top: 5px;
  width: 5px;
  right: -5px;
  display: block !important;
}

.tree-container /deep/ .el-tree-node {
  position: relative;
  padding-left: 13px;
}

.tree-container /deep/ .el-tree-node:before {
  width: 1px;
  height: 100%;
  content: '';
  position: absolute;
  top: -38px;
  bottom: 0;
  left: 0;
  right: auto;
  border-width: 1px;
  border-left: 1px solid #b8b9bb;
}

.tree-container /deep/ .el-tree-node:after {
  width: 13px;
  height: 13px;
  content: '';
  position: absolute;
  left: 0;
  right: auto;
  top: 12px;
  bottom: auto;
  border-width: 1px;
  border-top: 1px solid #b8b9bb;
}

.tree-container /deep/ .el-tree > .el-tree-node:before {
  border-left: none;
}

.tree-container /deep/ .el-tree > .el-tree-node:after {
  border-top: none;
}

.tree-container /deep/ .el-tree .el-tree-node:last-child:before {
  height: 50px;
}

.tree-container /deep/ .el-tree .el-tree-node__content {
  color: #000;
  font-size: 14px;
  padding-left: 0 !important;
}

.tree-container /deep/ .el-tree .el-tree-node__children {
  padding-left: 11.5px;
}

.tree-container /deep/ .el-tree .el-tree-node__content > label.el-checkbox {
  margin: 0 5px 0 5px !important;
}

.tree-container /deep/ .el-tree .el-tree-node__expand-icon {
  position: relative;
  z-index: 99;
}

.tree-container /deep/ .el-tree .el-tree-node__expand-icon.is-leaf {
  display: none;
}

.tree-container /deep/ .el-tree .leaf-node-line {
  width: 23px;
  height: 13px;
  content: '';
  position: absolute;
  left: 13px;
  right: auto;
  top: 12px;
  bottom: auto;
  border-width: 1px;
  border-top: 1px solid #b8b9bb;
}

.tree-container /deep/ .el-tree .el-tree-node__content:has(.is-leaf) {

  margin-left: 24px !important;
}

.el-form-item__label {
  padding: 0px !important;
}

.el-drawer__header {
  margin: 0px !important;
  padding: 16px !important;
}

.demo-drawer {
  width: 300px;
}

.file-view-box {
  height: calc(100vh - 126px);
}

.filter-container {
  border-bottom: 1px solid #cccccc;
  padding: 10px 3px 0px 3px;
  overflow: hidden;
  height: 50px;
}

.file-button-box {
  height: 36px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}

.file-button-box-selected {
  background: #1067de;
  color: #ffffff !important;
}

.gwbox:hover {
  background: #dcdcdc;
}

.file-button-box:hover {
  background: #dcdcdc;
}

.file-button-box-hover {
  background: #dcdcdc;
}

.file-button-img-box {
  width: 36px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.file-button-title {
  display: flex;
  align-items: center;
  flex: 1;
}
.file-button-img-DWI {
  background-position: 0px -1800px;
}
.file-button-img-EPM {
  background-position: 0px -1850px;
}
.file-button-img-SPM {
  background-position: 0px -1900px;
}
.file-button-img {
  width: 16px;
  height: 16px;
  background: url('/static/icons-small.png') no-repeat;
}

.file-button-right {
  font-size: 14px;
  width: 20px;
}

.components-container {
  position: relative;
  height: calc(100vh - 126px);
  margin: 0px;
  border: 2px solid #cccccc;
}

.left-container {
  /*background-color: #F38181;*/
  overflow-y: auto;
  height: 100%;
  padding: 3px;
}

/* 自定义滚动条样式 */
::-webkit-scrollbar {
  width: 8px; /* 宽度 */
}

::-webkit-scrollbar-track {
  background-color: #f1f1f1; /* 轨道颜色 */
}

::-webkit-scrollbar-thumb {
  background-color: #dcdcdc; /* 拇指颜色 */
  border-radius: 5px; /* 圆角 */
}

::-webkit-scrollbar-thumb:hover {
  background-color: #dcdcdc; /* 鼠标悬停时的颜色 */
}

.right-container {
  background-color: #FCE38A;
  height: 200px;
}

.top-container {
  background-color: #FCE38A;
  width: 100%;
  height: 100%;
}

.bottom-container {
  width: 100%;
  background-color: #95E1D3;
  height: 100%;
}

.file-button-img-dir {
  background-position: 0px -50px;
}

.file-button-img-image {
  background-position: 0px -250px;
}

.file-button-img-cad {
  background-position: 0px -200px;
}

.file-button-img-cadDir {
  background-position: 0px -150px;
}

.file-button-img-music {
  background-position: 0px -350px;
}

.file-button-img-vedio {
  background-position: 0px -400px;
}

.file-button-img-txt {
  background-position: 0px -450px;
}

.file-button-img-pdf {
  background-position: 0px -500px;
}

.file-button-img-gw {
  background-position: 0px -550px;
}

.file-button-img-world {
  background-position: 0px -1500px;
}

.file-button-img-exe {
  background-position: 0px -900px;
}

.file-button-img-zip {
  background-position: 0px -1150px;
}

.file-button-img-ppt {
  background-position: 0px -1400px;
}

.file-button-img-excel {
  background-position: 0px -1450px;
}


</style>
